home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / src / smtp / smtpd.sun.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-06-02  |  3.7 KB  |  193 lines

  1. /*
  2.  *   SMTPD.SUN.C
  3.  *
  4.  *    Special version for braindamaged SUN version of INETD.
  5.  *
  6.  *    Server process that is designed to be called by SUN/INETD,
  7.  *    the stupid Internet Daemon of Daemons.  It calls SMTPSRVR
  8.  *    with the proper arguments.
  9.  *
  10.  *    Expects arguments in form:
  11.  *
  12.  *    (1) smtpd sourcehost.sourceport
  13.  *        for SUN
  14.  *
  15.  *    (2) smtpd - smtpserver channel
  16.  *        for others or smart suns (better to use smtpd.4.3.c)
  17.  *
  18.  *        R E V I S I O N   H I S T O R Y
  19.  *   
  20.  */
  21.  
  22.  
  23. #include "util.h"
  24. #include <signal.h>
  25. #include <sys/socket.h>
  26. #include <netinet/in.h>
  27. #include <netdb.h>
  28.  
  29. extern int errno;
  30. extern char *chndfldir;
  31. extern char *rindex(), *sprintf();
  32. extern void bomb();    /* advance declaration -- routine in this file */
  33.  
  34. char localhost[256];
  35. char remotehost[256];
  36. char smtpserv[256] = "";
  37. char *channel =  "smtp";    /* not configurable on SUN */
  38.  
  39. main(argc,argv)
  40. int argc;
  41. char **argv;
  42. {
  43.     register int valid = 0;
  44.     int opt;
  45.  
  46.     if (argc >= 2)
  47.     {
  48.  
  49.     /* now which kind of inetd called us? */
  50.  
  51.     if (*argv[1] == '-') 
  52.         valid = setup1(argc-2,argv+2);
  53.     else 
  54.         valid = setup2(argc-1,argv+1);
  55.     }
  56.  
  57.     if (!valid)
  58.     {
  59.     /* bomb calls exit() */
  60.     bomb("Usage: %s [- smtpsrv channels] [rmthost.port]", argv[0]);
  61.     }
  62.  
  63.     if (smtpserv[0] == '\0')
  64.     {
  65.     /* only if not user configured... */
  66.     mmdf_init(argv[0]);
  67.     (void) sprintf(smtpserv,"%s/smtpsrvr",chndfldir);
  68.     }
  69.  
  70.     opt = 1;
  71.     (void) setsockopt (0, SOL_SOCKET, SO_KEEPALIVE, (char *)&opt, sizeof(opt));
  72.  
  73.     execl(smtpserv,"smtpsrvr",remotehost,localhost,channel,(char *)0);
  74.  
  75.     /* bomb calls exit() */
  76.     bomb("server exec error (%d)",errno);
  77. }
  78.  
  79. setup1(count,args)
  80. int count;
  81. char **args;
  82. {
  83.     register int i;
  84.     register struct hostent *he;
  85.     struct sockaddr_in rmt;
  86.     int size;
  87.  
  88.     for(i=0; i < count; i++)
  89.     {
  90.     switch(i)
  91.     {
  92.         case 0:
  93.         (void) strcpy(smtpserv,args);
  94.         break;
  95.  
  96.         case 1:
  97.         channel = args[i];
  98.         break;
  99.  
  100.         default:
  101.         break;
  102.     }
  103.     }
  104.  
  105.     /* now learn remote address */
  106.     size = sizeof(rmt);
  107.     if (getpeername(0,&rmt,&size) != 0)
  108.     return(0);
  109.  
  110.     he = gethostbyaddr((char *)&rmt.sin_addr,sizeof(rmt.sin_addr),rmt.sin_family);
  111.  
  112.     if ((he == NULL) || !isstr(he->h_name))
  113.     {
  114.     char *cp = (char *)&rmt.sin_addr.s_addr;
  115.  
  116.     (void)sprintf(remotehost,"%u.%u.%u.%u",
  117.         (int)cp[0],(int)cp[1],(int)cp[2],(int)cp[3]);
  118.     }
  119.     else
  120.     (void)strcpy(remotehost,he->h_name);
  121.  
  122.      /* now local host name */
  123.      size = sizeof(localhost);
  124.      if (gethostname(localhost,&size) != 0)
  125.     (void)strcpy(localhost,"LocalHost");
  126.  
  127.  
  128.     return(1);
  129. }
  130.  
  131. setup2(count,args)
  132. int count;
  133. char **args;
  134. {
  135.     register char *cp;
  136.     register struct hostent *he;
  137.     int size;
  138.     struct in_addr in;
  139.  
  140.     if (count != 1)
  141.     return(0);
  142.  
  143.     /* strip off trailing port value */
  144.     if ((cp = rindex(args[0],'.')) == 0)
  145.     return(0);
  146.  
  147.     *cp = 0;
  148.  
  149.     /* now get symbolic name if we can */
  150.  
  151.     (void) sscanf(args[0],"%x",&in.s_addr);
  152.     he = gethostbyaddr((char *)&in,4,AF_INET);
  153.  
  154.     if ((he != 0) && isstr(he->h_name))
  155.     (void)strcpy(remotehost,he->h_name);
  156.     else
  157.     (void)strcpy(remotehost,args[0]);
  158.  
  159.     printf("remotehost = %s\n",remotehost);
  160.  
  161.      size = sizeof(localhost);
  162.      if (gethostname(localhost,&size) != 0)
  163.     (void)strcpy(localhost,"LocalHost");
  164.  
  165.     printf("localhost = %s\n",localhost);
  166.  
  167.     return(1);
  168. }
  169.  
  170. /**************************************************************************/
  171. /* this works for both SUN and VAX -- more creativity may be required w/  */
  172. /* other types of machines.                                               */
  173. /**************************************************************************/
  174.  
  175. /* VARARGS1 */
  176. void bomb(fmt,args)
  177. char *fmt;
  178. int args;
  179. {
  180.     char buf[BUFSIZ];
  181.  
  182.     setbuf(stdout,buf);
  183.  
  184.     printf("451 ");
  185.  
  186.     _doprnt(fmt,&args,stdout);
  187.     printf("\r\n");
  188.  
  189.     (void) fflush(stdout);
  190.  
  191.     exit(99);
  192. }
  193.